package com.hxgis.common.conare;

import com.hxgis.common.base.StationAssociated;
import com.hxgis.common.base.StationCache;
import com.hxgis.common.entity.RainstormInfo;
import com.hxgis.common.entity.Station;
import com.hxgis.common.utils.CollectionUtils;
import java.util.*;

/**
 * 暴雨区域处理
 */
public class RainstormArea {

    private String araFlag;

    private Set<RainstormTime> rainstormTimes = new HashSet<RainstormTime>();

    public RainstormArea(RainstormTime rainstormTime) {
        rainstormTimes.add(rainstormTime);
        //生成区域标识
        araFlag = UUID.randomUUID().toString().replace("-","");
        rainstormTime.setAreaFlag(araFlag);
    }

    public void add(RainstormTime rainstormTime){
        rainstormTimes.add(rainstormTime);
        rainstormTime.setAreaFlag(araFlag);
    }

    public void merge(RainstormArea rainstormArea){
        rainstormTimes.addAll(rainstormArea.getRainstormTimes());
        setAraFlag(araFlag);
    }

    private void initAraFlag(){
        for (RainstormTime rainstormTime : rainstormTimes) {
            RainstormInfo rainstormInfo = rainstormTime.getRainstormInfos().get(0);
            String courseAreas = rainstormInfo.getCourseArea();
            if (courseAreas!=null)araFlag = courseAreas;
        }
        setAraFlag(araFlag);
        return;
    }

    public List<RainstormInfo> getRainstormInfoList(){
        initAraFlag();
        List<RainstormInfo> rainstormInfos = new ArrayList<RainstormInfo>();
        for (RainstormTime rainstormTime : rainstormTimes) {
            for (RainstormInfo rainstormInfo : rainstormTime.getRainstormInfos()) {
                rainstormInfo.setCourseArea(araFlag);
                rainstormInfos.add(rainstormInfo);
            }
        }
        return rainstormInfos;
    }

    //进行条件过滤
    public boolean check(RainstormTime rainstormTime){
        // 计算 时间是否在范围内
        List<RainstormTime> rainstormTimes = checkTime(rainstormTime);
        if (!CollectionUtils.isNotBlank(rainstormTimes)) {
            return false;
        }
        // 计算 计算是否在区域站点范围内
        return checkArea(rainstormTimes,rainstormTime);
    }

    /**
     * 校验时间是否存在交集
     * @param rainstormTime
     * @return
     */
    private List<RainstormTime> checkTime(RainstormTime rainstormTime){
        List<RainstormTime> rainstormTimeList = new ArrayList<RainstormTime>();
        for (RainstormTime time : rainstormTimes) {
            if (time.checkTime(rainstormTime)){
                rainstormTimeList.add(time);
            }
        }
        return rainstormTimeList;
    }

    /**
     * 校验区域是否存在交集
     * @param rainstormTimes
     * @param rainstormTime
     * @return
     */
    private boolean checkArea(List<RainstormTime> rainstormTimes,RainstormTime rainstormTime){
        StationAssociated stationAssociated =
                StationCache.getScopeStationList(rainstormTime.getStationNo());
        for (RainstormTime time : rainstormTimes) {
            Station associated = stationAssociated.getStationAssociated(time.getStationNo());
            if (associated != null){
                return true;
            }
        }
        return false;
    }

    public Set<RainstormTime> getRainstormTimes() {
        return rainstormTimes;
    }

    public void setRainstormTimes(Set<RainstormTime> rainstormTimes) {
        this.rainstormTimes = rainstormTimes;
    }

    public String getAraFlag() {
        return araFlag;
    }

    public void setAraFlag(String araFlag) {
        this.araFlag = araFlag;
        for (RainstormTime rainstormTime : rainstormTimes) {
            rainstormTime.setAreaFlag(araFlag);
        }
    }
}
